// API配置 - 根据环境变量设置不同的请求地址
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || 'http://localhost:3000/qx/console/v1'

// 统一响应结构
export interface ApiResponse<T = any> {
  code: number
  msg: string
  data: T | null
}

// 错误处理函数
const handleApiResponse = async <T>(response: Response): Promise<T> => {
  const result: ApiResponse<T> = await response.json()
  
  if (result.code === 200) {
    return result.data as T
  } else {
    // 使用ElMessage显示错误消息
    if (typeof window !== 'undefined' && (window as any).ElMessage) {
      (window as any).ElMessage.error(result.msg || '请求失败')
    } else {
      console.error('API错误:', result.msg)
    }
    throw new Error(result.msg || '请求失败')
  }
}

export {
  API_BASE_URL,
  handleApiResponse
}